/*
 * @Author: 周江英 10873108+zhou-jiangying@user.noreply.gitee.com
 * @Date: 2023-01-12 22:45:36
 * @LastEditors: 周江英 10873108+zhou-jiangying@user.noreply.gitee.com
 * @LastEditTime: 2023-02-12 20:45:24
 * @FilePath: \vue-shop\src\router.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import Vue from 'vue' // 导入vue组件
import Router from 'vue-router' // 导入路由
import Login from './components/Login.vue'
import Home from './components/Home.vue'
import router from './router'
import Welcome from './components/Welcome.vue'
import Users from './components/user/Users.vue'
import Rights from './components/power/Rights.vue'
import Roles from './components/power/Roles.vue'
import Cate from './components/goods/Cate.vue'
import Params from './components/goods/Params.vue'
import List from './components/goods/List.vue'
import Add from './components/goods/Add.vue'

Vue.use(Router) // 全局使用路由

export default new Router({
  routes: [
    { path: '/', redirect: '/login' }, // 重定向路由地址
    { path: '/login', component: Login }, // 定义路由规则
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/rights', component: Rights },
        { path: '/roles', component: Roles },
        { path: '/categories', component: Cate },
        { path: '/params', component: Params },
        { path: '/goods', component: List },
        { path: '/goods/add', component: Add }
      ]
    }
  ]
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next() 放行 next('地址') 强制跳转
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token') // 获取到token值
  if (!tokenStr) return next('/login') // 判断token 如果不等于token则强制跳转指定页面
  next()
})
